/**
 * 
 */
package com.scpii.universal.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.xwork.StringUtils;
import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.ShopPOJO;
import com.scpii.universal.hibernate.dao.EnterpShopDao;
import com.scpii.universal.hibernate.entity.ContentTab;
import com.scpii.universal.hibernate.entity.EnterpShopTab;
import com.scpii.universal.pns.dao.TagValuesDao;
import com.scpii.universal.pns.dao.TagsDao;
import com.scpii.universal.pns.entity.TagValues;
import com.scpii.universal.pns.entity.Tags;
import com.scpii.universal.service.AppContentService;
import com.scpii.universal.service.EnterpShopService;

/**
 * genericWeb
 * @author weny
 * 2012-7-8下午09:42:32
 */
@Service
public class EnterpShopServiceImpl extends
		BaseServiceImpl<EnterpShopTab, Integer> implements EnterpShopService {
	@Resource
	private EnterpShopDao enterpShopDao;
	@Resource
	private TagsDao tagsDao;
	@Resource
	private TagValuesDao tagValuesDao;
	@Resource
	private AppContentService appContentService;

	@Resource
	public void setBaseDao(EnterpShopDao enterpShopDao) {
		super.setBaseDao(enterpShopDao);
	}

	@Override
	public void updateShop(EnterpShopTab shop, ShopPOJO shopPOJO) {
		Integer shopId;
		Integer initShopId = shop.getIntShopId();
		if (shop.getIntShopId() != null) {
			enterpShopDao.update(shop);
			shopId = shop.getIntShopId();
		} else {
			shopId = enterpShopDao.save(shop);
		}
		long targetId;
		long objectId;
		if("c".equals(shopPOJO.getFrom())){//以内容的方式添加门店
			Integer intContentId = saveContentFromShop(shop,initShopId,shopPOJO);
			shop.setIntContentId(intContentId);
			targetId = GenericConstants.CATE_ID_PRE + shopId;
			objectId = GenericConstants.CATE_ID_PRE+shop.getIntCateId();
		} else {
			targetId = GenericConstants.SHOP_ID_PRE+shopId;
			objectId = GenericConstants.SHOP_ID_PRE+shop.getIntAppId();
		}
		Date date = new Date();
		String selectedTagsContainer = shopPOJO.getSelectedTagsContainer();
		tagValuesDao.deleteByTargetId(targetId);
		if (!StringUtils.isBlank(selectedTagsContainer)) {
			selectedTagsContainer = selectedTagsContainer.substring(1);
			String[] selectedTags = selectedTagsContainer.split(",");
			for (String string : selectedTags) {
				saveTagValues(date, targetId, Long.parseLong(string));
			}
		}
		String delTagsContainer = shopPOJO.getDelTagsContainer();
		if (!StringUtils.isBlank(delTagsContainer)) {
			delTagsContainer = delTagsContainer.substring(1);
			String[] delTags = delTagsContainer.split(",");
			for (String string : delTags) {
				tagValuesDao.deleteByTagId(Long.parseLong(string));
				tagsDao.delete(Long.parseLong(string));
			}
		}
		String newTagsContainer = shopPOJO.getNewTagsContainer();
		if (!StringUtils.isBlank(newTagsContainer)) {
			newTagsContainer = newTagsContainer.substring(1);
			String[] newTags = newTagsContainer.split(",");
			for (String string : newTags) {
				Tags tag = new Tags();
				tag.setTitle(string);
				tag.setCreateDt(date);
				tag.setUpdateDt(date);
				tag.setObjectId(objectId);
				long tagId = tagsDao.save(tag);
				saveTagValues(date, targetId, tagId);
			}
		}
	}
	
	private Integer saveContentFromShop(EnterpShopTab shop,Integer initShopId, ShopPOJO shopPOJO) {
		ContentTab contentTab = getContentInfoByShop(shop);
		contentTab.setIntResourceId((long)shop.getIntShopId());
		if (initShopId != null) {
			return appContentService.updateContentDetail(shopPOJO.getIntContentId(), contentTab, shopPOJO.getImgList(), shopPOJO.getAttList(), shopPOJO.getDeletedImgIds());
		} else {
			contentTab.setIntResourceId((long)shop.getIntShopId());
			return appContentService.saveContent(shopPOJO.getUserInfoDto(), shop.getIntCateId(), contentTab, shopPOJO.getImgList(), shopPOJO.getAttList(), shopPOJO.getContext());
		}
	}
	
	private ContentTab getContentInfoByShop(EnterpShopTab shop){
		ContentTab content = new ContentTab();
		content.setVarContentName(shop.getVarShopName());
		content.setVarContentDescript(shop.getVarDesc());
		content.setVarContentImgUrl(shop.getVarIconUrl());
		return content;
	}
	
	private void saveTagValues(Date date,long targetId,Long tagId){
		TagValues tagValue = new TagValues();
		tagValue.setCreateDt(date);
		tagValue.setTargetId(targetId);
		tagValue.setTagId(tagId);
		tagValue.setUpdateDt(date);
		tagValuesDao.save(tagValue);
	}

	public void update(EnterpShopTab obj,String fromUrl,boolean isBinded) {
		enterpShopDao.update(obj);
		if("c".equals(fromUrl)){
			tagValuesDao.deleteByTargetId(GenericConstants.CATE_ID_PRE+obj.getIntShopId());
			appContentService.deleteContentDetail(obj.getIntContentId(), isBinded);
		} else {
			tagValuesDao.deleteByTargetId(GenericConstants.SHOP_ID_PRE+obj.getIntShopId());
		}
	}

	@Override
	public List<Tags> getUsedTags(Long targetId) {
		return tagsDao.getUsedTags(targetId);
	}

	@Override
	public List<Tags> getUsefullTags(Long objectId) {
		return tagsDao.getAllTags(objectId);
	}

	@Override
	public List<Tags> getTagsAndUseNum(Long objectId) {
		return tagsDao.getTagsAndUseNum(objectId);
	}

	@Override
	public void deleteTag(Long tagId) {
		tagValuesDao.deleteByTagId(tagId);
		tagsDao.delete(tagId);
	}

	@Override
	public void updateTag(Long tagId, String Value) {
		StringBuffer hql = new StringBuffer();
		hql.append("update Tags t set ");
		hql.append("t.title=:title,t.updateDt=:updateDt");
		hql.append(" where t.id=:id");
		Map<String,Object> argMaps = new HashMap<String,Object>();
		argMaps.put("title",Value);
		argMaps.put("id",tagId);
		argMaps.put("updateDt",new Date());
		tagsDao.hqlOperation(hql.toString(), argMaps);
	}

	@Override
	public void saveTag(String value,Long objectId) {
		if (!StringUtils.isBlank(value)) {
			value = value.substring(1);
			String[] newTags = value.split(",");
			Date date = new Date();
			for (String string : newTags) {
				Tags tag = new Tags();
				tag.setTitle(string);
				tag.setCreateDt(date);
				tag.setUpdateDt(date);
				tag.setObjectId(objectId);
				tagsDao.save(tag);
			}
		}
	}

	@Override
	public void updateShopTags(String tags, String shops) {
		Date date = new Date();
		String[] shopIds = shops.split(",");
		Map<String,String> shopTags = tagValuesDao.getTagIdsByTargetId(shops);
		String existTags = null;
		String[] tagIds = null;
		for(String shopId:shopIds){
			existTags = shopTags.get(shopId);
			tagIds = tags.split(",");
			for (String tagId : tagIds) {
				if(existTags!=null && existTags.contains(tagId))
					continue;
				saveTagValues(date,Long.parseLong(shopId),Long.parseLong(tagId));
			}
		}
	}
}
